﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;

/*
 * 
 * Search And Extrac
 * By: Nathan Adams
 * 
 * Description: Searches for a regular expression and puts the results into the clipboard
 * 
 * Version 1.0
 * 
 * 
 */

namespace searchandextract
{
    class searchandextract
    {
        //private string[] args;
        private string location;
        private string regex;
        private string files;
        private bool subfolder;
        private bool clipboard;

        public searchandextract(string args)
        {

        }

        public searchandextract(string[] args)
        {
            // TODO: Complete member initialization
            //this.args = args;
            this.subfolder = false;
            this.clipboard = false;
            string para = "";
            string value = "";
            foreach (string arg in args)
            {
                if (para == "")
                {
                    para = arg;
                    if (para == "-s" || para == "-c")
                    {
                        this.parseOptions(para, value);
                        para = "";
                        continue;
                    }
                }
                else
                {
                    value = arg;
                    this.parseOptions(para, value);
                    para = "";
                }
            }
        }
        public void parseOptions(string para, string value)
        {
            switch (para)
            {
                case "-f":
                    this.files = value;
                    break;
                case "-l":
                    this.location = value;
                    break;
                case "-r":
                    this.regex = value;
                    break;
                case "-s":
                    this.subfolder = true;
                    break;
                case "-c":
                    this.clipboard = true;
                    break;
                default:
                    throw new Exception("Unrecongized parameter");
            }
        }
        public List<string> search()
        {
            List<string> results = new List<string>();
            string [] files;
            string filecont;
            Match m;
            List<string> restmp = new List<string>();
            Regex rg = new Regex(this.regex);
            if (this.subfolder)
                files = Directory.GetFiles(this.location, this.files, SearchOption.AllDirectories);
            else
                files = Directory.GetFiles(this.location, this.files, SearchOption.TopDirectoryOnly);
            if (files.Length == 0)
            {
                if (this.clipboard)
                    Clipboard.SetText("");
                return results;
            }
            else
            {
                foreach(string file in files)
                {
                    filecont = File.ReadAllText(file);
                    //mc = rg.Matches(filecont);
                    restmp.Clear();
                    m = rg.Match(filecont);
                    while (m.Success)
                    {
                        restmp.Add(m.Value);
                        m = m.NextMatch();
                    }
                    
                    results.Add(String.Join(",", restmp.ToArray()));
                }
                if (this.clipboard && results.Count > 0 && results[0] != "")
                    Clipboard.SetText(String.Join("\r\n", results.ToArray()));
                return results;
            }
        }

    }
}
